﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace HomeWork
{
    [Serializable]
    public class Order
    {
        public int orderID;
        string client;
        double price;
        Dictionary<string, int> production = new Dictionary<string, int>();


        public void addProduction(int amount, int price, string production)
        {
            this.price += price * amount;
            bool ifExist = false;
            foreach(string key in this.production.Keys)
            {
                if (key == production)
                {
                    int amount_1 = this.production[key] + amount;
                    this.production[key] = amount_1;
                    ifExist = true;
                }
            }
            if(!ifExist)
            {
                this.production.Add(production, amount);
            }
        }

        public void deleteProduction(int amount, int price, string production)
        {
            bool ifExist = false;
            foreach(string key in this.production.Keys)
            {
                if(key == production)
                {
                    this.price -= price * amount;
                    int amount_1 = this.production[key] - amount;
                    if (amount_1 <= 0) this.production.Remove(key);
                    this.production[key] = amount_1;
                    ifExist = true;
                }
            }
            if (!ifExist)
                Console.WriteLine("无此商品");
        }
        public override bool Equals(object obj)
        {
            return obj is Order order &&
                   orderID == order.orderID &&
                   client == order.client &&
                   price == order.price &&
                   production == order.production;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(orderID, client, price, production);
        }

        public override string ToString()
        {
            string[] array;
            StringBuilder Production = new StringBuilder();
            array = new string[production.Keys.Count];
            production.Keys.CopyTo(array, 0);
            for (int i = 0; i < production.Keys.Count; i++)
            {
                Production.Append(array[i]);
                Production.Append("   ");
            }
            return (orderID.ToString() + "     " +
                    client + "     " +
                    Production.ToString() + "     " +
                    price.ToString() + System.Environment.NewLine);
        }

        public Order(int orderID, string client, double price, Dictionary<string, int> production)
        {
            this.orderID = orderID;
            this.client = client;
            this.price = price;
            this.production = production;
        }
        public Order() { }
    }

    public class OrderDetails
    {
        public int orderID;
        int price;
        int amount;
        string production;
        public OrderDetails(int orderID, int price, int amount, string production)
        {
            this.orderID = orderID;
            this.price = price;
            this.amount = amount;
            this.production = production;
        }

        public bool addDetails(int amount, int price, string production, int orderID)
        {
            if (this.production == production && orderID == this.orderID)
            {
                this.amount += amount;
                return true;
            }
            else
                return false;
        }

        public bool deleteDetails(int amount, int price, string production, int orderID)
        {
            if (this.production == production && orderID == this.orderID && this.amount - amount <= 0)
            {
                this.amount -= amount;
                return true;
            }
            else
                return false;
        }

        public override bool Equals(object obj)
        {
            return obj is OrderDetails details &&
                   orderID == details.orderID &&
                   price == details.price &&
                   amount == details.amount &&
                   production == details.production;
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(orderID, price, amount, production);
        }

        public override string ToString()
        {
            return (orderID.ToString() + "     " +
                    production + "     " +
                    price + "     " +
                    amount + System.Environment.NewLine);
        }
    }

    public class OrderService
    {
        List<Order> orderList = new List<Order>();
        List<OrderDetails> orderDetailsList = new List<OrderDetails>();

        public void addOrder(int orderID, string client,
            Dictionary<string, int> production, Dictionary<string, int> price, Order orders)
        {
            string[] productionArray = production.Keys.ToArray<string>();
            string[] productionArray_2 = price.Keys.ToArray<string>();
            int[] amountArray = production.Values.ToArray<int>();
            int[] priceArray = price.Values.ToArray<int>();
            int totalPrice_1 = 0;
            bool ifValid = true;
            for (int m = 0; m < productionArray.Length; m++)
                for (int n = 0; n < priceArray.Length; n++)
                {
                    if (productionArray[m] == productionArray_2[n])
                    {
                        totalPrice_1 += amountArray[m] * priceArray[n];
                    }
                }
            Order order = new Order(orderID, client, totalPrice_1, production);
            orders = order;
            for (int m = 0; m < productionArray.Length; m++)
                for (int n = 0; n < priceArray.Length; n++)
                {
                    if (productionArray[m] == productionArray_2[n])
                    {
                        OrderDetails details = new OrderDetails(orderID, priceArray[n], amountArray[m], productionArray[m]);
                        foreach (Order order1 in orderList)
                        {
                            if (order1.Equals(order))
                            {
                                ifValid = false;
                                Console.WriteLine("重复订单");
                            }
                        }
                        if(ifValid)
                        {
                            orderDetailsList.Add(details);
                            Console.WriteLine("添加一次明细");
                        }
                    }
                }
            if(ifValid)
            {
                orderList.Add(order);
                Console.WriteLine("成功添加一次订单");
            }
        }
        public void deleteOrder(int orderID)
        {
            bool ifExist = false;
            Order[] array_1 = orderList.ToArray();
            foreach(Order order in array_1)
            {
                if (order.orderID == orderID)
                {                            
                    ifExist = true;
                    orderList.Remove(order);
                    OrderDetails[] array = orderDetailsList.ToArray();
                    foreach (OrderDetails details in array)
                    {
                       if (details.orderID == orderID)
                        {
                            orderDetailsList.Remove(details);
                            Console.WriteLine("已成功删除");
                        }
                    }
                }
            }
            if(ifExist == false)
                Console.WriteLine("无此订单");
        }
        public void findOrder(Order order, List<OrderDetails> details, int orderID)
        {
            foreach (Order ord in orderList)
            {
                if (orderID == ord.orderID)
                {
                    order = ord;
                    Console.WriteLine("已成功找到");
                }
            }
            foreach(OrderDetails detail in orderDetailsList)
            {
                if (detail.orderID == orderID)
                    details.Add(detail);
            }
        }
           
        public void Export()
        {
            Order[] array = orderList.ToArray();
            Console.WriteLine("serilized into an XML file");
            XmlSerializer xmlserializer = new XmlSerializer(typeof(Order[]));
            using(FileStream fs = new FileStream("s.xml", FileMode.Create))
            {
                xmlserializer.Serialize(fs, array);
            }
            Console.WriteLine(File.ReadAllText("s.xml"));
        }

        public void Import(Order[] order)
        {
            Console.WriteLine("deserialized from an XML file");
            XmlSerializer xmlserializer = new XmlSerializer(typeof(Order[]));
            using (FileStream fs = new FileStream("s.xml", FileMode.Open))
            {
                order = (Order[])xmlserializer.Deserialize(fs);
                Console.WriteLine("already deserialized");
            }
        }
        public void changeOrder(int orderID)
        {
            Order[] array = orderList.ToArray();
            foreach(Order order in array)
            {
                if(orderID == order.orderID)
                {
                    Console.WriteLine("请输入想改的商品内容：是加还是减？y或Y为加，n或N为减");
                    String str = Console.ReadLine();
                    if(str == "y" || str == "Y")
                    {
                        Console.WriteLine("请输入商品名、订购数量和单价");
                        string production;
                        int amount, price;
                        Console.WriteLine("商品名：");
                        production = Console.ReadLine();
                        Console.WriteLine("数量：");
                        amount = System.Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("单价：");
                        price = System.Convert.ToInt32(Console.ReadLine());
                        order.addProduction(amount, price, production);
                        OrderDetails[] array_1 = orderDetailsList.ToArray();
                        foreach(OrderDetails details in array_1)
                        {
                             if(details.addDetails(amount, price, production, orderID))
                            {
                                OrderDetails detail = new OrderDetails(orderID, price, amount, production);
                                orderDetailsList.Add(detail);
                            }
                        }                        
                    }
                    if(str == "n" || str == "N")
                    {
                        int amount, price;
                        Console.WriteLine("商品名：");
                        string production = Console.ReadLine();
                        Console.WriteLine("数量：");
                        amount = System.Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("单价：");
                        price = System.Convert.ToInt32(Console.ReadLine());
                        order.deleteProduction(amount, price, production);
                        OrderDetails[] array_2 = orderDetailsList.ToArray();
                        foreach(OrderDetails details in array_2)
                        {
                            details.deleteDetails(amount, price, production, orderID);
                        }
                    }
                }
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();
            dic.Add("铅笔", 3);
            dic.Add("橡皮", 2);
            Dictionary<string, int> dic_2 = new Dictionary<string, int>();
            dic_2.Add("钢笔", 5);
            Dictionary<string, int> dic_3 = new Dictionary<string, int>();
            dic_3.Add("手写纸", 10);
            Dictionary<string, int> dic2 = new Dictionary<string, int>();
            dic2.Add("铅笔", 1);
            dic2.Add("橡皮", 1);
            Dictionary<string, int> dic2_2 = new Dictionary<string, int>();
            dic2_2.Add("钢笔", 5);
            Dictionary<string, int> dic2_3 = new Dictionary<string, int>();
            dic2_3.Add("手写纸", 1);
            string client = "张三";
            string client_2 = "李四";
            string client_3 = "王五";
            int orderID = 1;
            int orderID_2 = 2;
            int orderID_3 = 3;
            int orderID_4 = 4;
            Order orders = new Order();
            OrderService service = new OrderService();
            service.addOrder(orderID, client, dic, dic2, orders);
            service.addOrder(orderID_2, client_2, dic_2, dic2_2, orders);
            service.addOrder(orderID_3, client_3, dic_3, dic2_3, orders);
            service.deleteOrder(orderID_3);
            service.deleteOrder(orderID_4);

            Order order = new Order();
            List<OrderDetails> list = new List<OrderDetails>();
            service.findOrder(order, list, orderID_2);
            service.changeOrder(orderID);
        }
    }
}
